package ch05
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;

	import utils.Ball;

	/**
	 * 双轴加速度
	 * @author sjp
	 *
	 */
	public class Acceleration3 extends Sprite
	{
		private var ball:Ball;
		private var vx:Number=0;
		private var vy:Number=0;
		private var ax:Number=0;
		private var ay:Number=0;
		private var text:TextField=new TextField();

		public function Acceleration3()
		{
			init();
		}

		private function init():void
		{
			ball=new Ball;
			addChild(ball);
			ball.x=stage.stageWidth / 2;
			ball.y=stage.stageHeight / 2;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			this.addChild(text);
		}

		private function onKeyDown(event:KeyboardEvent):void
		{
			switch (event.keyCode)
			{
				case Keyboard.LEFT:
					ax=-0.2;
					break;
				case Keyboard.RIGHT:
					ax=0.2;
					break;
				case Keyboard.UP:
					ay=-0.2;
					break;
				case Keyboard.DOWN:
					ay=0.2;
					break;
				default:
					break;
			}
		}

		private function onKeyUp(event:KeyboardEvent):void
		{
			ax=0;
			ay=0;
		}

		private function onEnterFrame(event:Event):void
		{
			vx+=ax;
			vy+=ay;
			ball.x+=vx;
			ball.y+=vy;
			text.text="x加速度:" + ax + "\n" + "y加速度:" + ay + "\n" + "球x的速度:" + vx + "\n" + "球y的速度:" + vy + "\n" + "球的X位置:" + ball.x + "\n" + "球的y位置:" + ball.y;
		}
	}
}